﻿
using EvaluationSystem.Dto;
using EvaluationSystem.Models.Views;
using EvaluationSystem.Utils;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EvaluationSystem.Apis
{
    [ApiController]
    public class PeopleController : ApiBaseController<PeopleController>
    {
        /// <inheritdoc />
        public PeopleController(ILogger<PeopleController> logger, MyDbContext dbContext) : base(logger, dbContext)
        {
        }

        [HttpGet]
        public IActionResult Test()
        {
            return Content("ok");
        }

        /// <summary>
        /// 学生评分
        /// </summary>
        /// <param name="scId"></param>
        /// <param name="evals"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Eval(int scId, string evals)
        {
            if (string.IsNullOrWhiteSpace(evals))
            {
                return Ok(ResultDto.Fail(msg: "评价信息不能为空"));
            }
            else
            {
                var obj = await _dbContext.StudentCourse.FirstOrDefaultAsync(x => x.ScId == scId);
                if (obj != null)
                {
                    obj.Evals = evals;
                    obj.Score = ScoreUtil.getScore(evals);
                    var num = await _dbContext.SaveChangesAsync();
                    if (num <= 0)
                    {
                        return Ok(ResultDto.Fail(msg: "评价失败"));
                    }
                    return Ok(ResultDto.Ok());
                }
                return Ok(ResultDto.Fail(msg: "此选课不存在"));
            }
        }

        /// <summary>
        /// 表单数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ChartData()
        {
            List<Eval_Course> ec_list = await _dbContext.Eval_Course.ToListAsync(); // 学生评分实体
            var tuplesChartData = ScoreUtil.getChartData(ec_list);
            if (tuplesChartData != null)
            {
                (List<string> teacherArray, List<float> scoreArray) = tuplesChartData;
                return Ok(ResultDto.Ok(data: new
                {
                    teachers = teacherArray,
                    scores = scoreArray
                }));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: ""));
            }
        }

        /// <summary>
        /// 表单数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ChartData2()
        {
            var sc_list = await _dbContext.StudentCourse
                .Include(t => t.TeacherWithCourses)
                .ThenInclude(t => t.Teacher)
                .ToListAsync();
            var tuplesChartData = ScoreUtil.getChartData2(sc_list);
            if (tuplesChartData != null)
            {
                (List<string> teacherArray, List<float> scoreArray) = tuplesChartData;
                return Ok(ResultDto.Ok(data: new
                {
                    teachers = teacherArray,
                    scores = scoreArray
                }));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: ""));
            }
        }
    }
}
